package ml.features;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;

import ml.Data;

import parser.ParserAnnotation;

public class SentLengthExtractor extends FeatureExtractor {
	private int longestSentence;

	public SentLengthExtractor(int offset, int longestSentence) {
		super(offset);
		this.longestSentence = longestSentence;
	}

	public SentLengthExtractor(int offset) throws FileNotFoundException {
		super(offset);
		Scanner scr = new Scanner(new File("features/slength"));
		longestSentence = scr.nextInt();
		scr.close();
	}

	public int nbrColumns() {
		return 1;
	}

	@Override
	public String[] wekaDescription() {
		return new String[] { "\"SENT LENGTH\" numeric" };
	}

	@Override
	public List<Data> extract(ParserAnnotation pa, int sentence) {
		double length = pa.getTokens(sentence).size();
		length = length / longestSentence * 2 - 1;
		Data data = new Data(length, offset);
		return Arrays.asList(new Data[] { data });
	}

	@Override
	public List<Data>[] extractSequence(ParserAnnotation pa, int sentence) {
		throw new Error(
				"Cannot use sentence length as a feature with sequences.");
	}

	@Override
	public void store() throws IOException {
		File out = new File("features/slength");
		BufferedWriter bw = new BufferedWriter(new FileWriter(out));
		bw.append("" + longestSentence);
		bw.flush();
		bw.close();
	}

}
